const app = getApp()

// components/authorization/index.js
Component({
  data: {
    isLogin: false
  },
  //生命周期
  lifetimes: {
    attached () {
      const isLogin = !!app.token
      // 获取页面路径
      const route = getCurrentPages().pop().route
      const currentPage = getCurrentPages().pop()

      if (!isLogin) {
        //如果没有登录，用空函数覆盖生命周期函数
        currentPage.onLoad = () => {}
        currentPage.onShow = () => {}
        //跳转到登录页
        wx.redirectTo({
          url: '/pages/login/index?redirectURL=' + route
        })
      }
      this.setData({ isLogin })
    }
  }
})
